package com.codeoss.framework.view.parser;

import org.springframework.stereotype.Service;

import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Container;
import com.codeoss.framework.view.component.DataGrid;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;


@Service("datagridParser")
public class DataGridParser extends AbstractComponentParser {
	@Override
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode,Component component) {
		super.setAttributes(parseContext, objectNode, component);
		DataGrid dataGrid=(DataGrid)component;
		
		ObjectNode propertyMapNode=(ObjectNode)objectNode.get("propertyMap");
		if(propertyMapNode!=null) {
			JsonNode jsonNode=propertyMapNode.get("dataSource");
			if(jsonNode!=null) {
				String dataSource=propertyMapNode.get("dataSource").asText();
				dataGrid.setDataSource(dataSource);
			}
	
			jsonNode=propertyMapNode.get("pageable");
			if(jsonNode!=null) {
				boolean pageable=propertyMapNode.get("pageable").asBoolean();
				dataGrid.setPageable(pageable);
			}
			
			jsonNode=propertyMapNode.get("onPagination");
			if(jsonNode!=null) {
				Expression expression=ExpressionUtil.convertToExpression(jsonNode.asText(), Expression.EXPRESSION_TYPE_SPRING);
				dataGrid.setOnPagination(expression);
			}
		}
	}
	
	@Override
	protected void parseChildren(IParseContext parseContext, ObjectNode objectNode,Container container) {
		DataGrid dataGrid=(DataGrid)container;
		parseContext.setAttribute("dataSource", dataGrid.getDataSource());
		super.parseChildren(parseContext, objectNode, container);
		parseContext.removeAttribute("dataSource");
	}
	
	@Override
	protected Component createComponent(IParseContext parseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		return new DataGrid();
	}

}
